package com.eliansun.crawler.pipeline;

import com.alibaba.fastjson.JSON;
import com.eliansun.service.domain.SpiderCommodity;
import com.eliansun.service.mapper.SpiderCommodityMapper;
import com.eliansun.service.service.ISpiderCommodityService;
import com.eliansun.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 通用持久化操作
 */
@Component("myPipeline")
public class AbstractPipeline implements Pipeline {

    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPipeline.class);

    @Autowired
    private SpiderCommodityMapper spiderCommodityMapper;

    @Override
    public void process(ResultItems resultItems, Task task) {

        Map<String, Object> map = (Map<String, Object>) resultItems.get("result");
        if (map == null) {
            return; //search页面
        }
        if (map.get("productName") == null) {
            LOGGER.info("-----------无效页面--------");
            return; //无效页面
        }
       spiderCommodityMapper.insert(getSpiderCommodity(map,task));

    }


    /**
     * 采集数据解析
     */
    private static SpiderCommodity getSpiderCommodity(Map<String,Object> map, Task task){
        SpiderCommodity commodity = new SpiderCommodity();

        //mongo数据
        String json = task.getUUID();
        Map<String, Object> object = (Map<String, Object>) JSON.parse(json);
        commodity.setProduceArea(object.get("produceArea").toString());
        commodity.setProvinceId(object.get("provinceId").toString());
        commodity.setSpiderJobId(Integer.parseInt(object.get("spiderJobId").toString()));
        commodity.setTerrace(object.get("terrace").toString());
        commodity.setKeyWord(object.get("keyWord").toString());
        commodity.setBatch(object.get("batch").toString());
        commodity.setMainUrl(object.get("mainUrl").toString());

        //采集数据
        commodity.setBrand(map.get("brand") == null ? "" : map.get("brand").toString());
        commodity.setExecutiveStandard(map.get("executiveStandard") == null ? "" : map.get("executiveStandard").toString());
        commodity.setMedicalDeviceNo(map.get("medicalDeviceNo") == null ? "" : map.get("medicalDeviceNo").toString());
        commodity.setNationality(map.get("nationality") == null ? "" : map.get("nationality").toString());
        commodity.setProductCompany(map.get("productCompany") == null ? "" : map.get("productCompany").toString());
        commodity.setProductName(map.get("productName").toString());
        commodity.setSpec(map.get("spec") == null ? "" : map.get("spec").toString());
        commodity.setStore(map.get("store") == null ? "" : map.get("store").toString());
        commodity.setUrl(map.get("url").toString());
        commodity.setVersion(map.get("version") == null ? "" : map.get("version").toString());
        commodity.setPrice(map.get("price") == null ? BigDecimal.valueOf(0) : BigDecimal.valueOf(Double.valueOf(map.get("price").toString())));

        return commodity;
    }


    private static boolean existsProduct(String key, String productName) {
        int trueCount = 0;
        char[] strs = productName.toCharArray();
        for (char r : strs) {
            if (!StringUtil.isNumeric(String.valueOf(r))) {
                if (key.indexOf(r) != -1) {
                    trueCount++;
                }
            }
        }
        if (key.length() > 2) {
            if (trueCount >= 3) {
                return true;
            }
        }
        if (key.length() == 2) {
            if (trueCount >= 2) {
                return true;
            }
        }
        return false;
    }

}
